package com.preskool.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.preskool.service.StudentService;
import com.preskool.dto.ActionResult;
import com.preskool.po.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * @Author kennyzzz
 * @Data 2021/1/6 16:14
 */
@RestController
@Slf4j
public class StudentController {
    @Autowired(required = false)
    private StudentService studentService;

    //通过id单查
    @RequestMapping(value = "/selectById",method = RequestMethod.GET)
    public ActionResult selectById(int id){
        System.out.println("要查的id是:"+id);
        Student student = studentService.selectById(id);
        System.out.println(student);
        if (student!=null){
            return new ActionResult(200,"查询到了",student);
        }else{
            return new ActionResult(205,"未查询到",null);
        }

    }


    //添加学生
    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    public ActionResult addStudent(@RequestBody Student student){
        Boolean aBoolean = studentService.addStudent(student);
        if (aBoolean==true){
            return new ActionResult(200,"添加成功",null);
        }else{
            return new ActionResult(205,"添加失败",null);
        }
    }


    //通过id删除学生
    @RequestMapping(value = "/deleteStudent",method = RequestMethod.POST)
    public ActionResult deleteStudent(int id){
        Boolean aBoolean = studentService.deleteStudent(id);
        if (aBoolean==true){
            return new ActionResult(200,"删除成功",null);
        }else{
            return new ActionResult(205,"删除失败",null);
        }
    }


    //通过id修改学生
    @RequestMapping(value = "/updateStudent",method = RequestMethod.POST)
    public ActionResult updateStudent(@RequestBody int id){
        Boolean aBoolean = studentService.updateStudent(id);
        if (aBoolean==true){
            return new ActionResult(200,"修改成功",null);
        }else{
            return new ActionResult(205,"修改失败",null);
        }
    }

    //分页查询
    @RequestMapping(value = "/findPage",method = RequestMethod.GET)
    public List<Student> findPage(@RequestParam(defaultValue = "1")Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<Student> list = studentService.findPage(currentPage, pageSize);
        //获取分页信息
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        System.out.println("total:"+pageInfo.getTotal());
        System.out.println("pages:"+pageInfo.getPages());
        System.out.println("pageSize:"+pageInfo.getPageSize());
        return list;

    }
}
